<template>
    <view>
        <web-view :src="webviewSrc" @message="onMessage" id="webview_con"></web-view>
    </view>
</template>

<script setup lang="ts">
import { ref } from "vue"
import { onLoad, onShow, onReachBottom, onPullDownRefresh, onBackPress } from "@dcloudio/uni-app"
import { addFrontErr } from "@/api/base";
import interact from "@/utils/interact";
import { useStore } from "vuex";
const Store = useStore();

const webviewSrc = ref("")
let webViewContext:any;
onLoad((options: any) => {
    // #ifdef H5
    location.replace(decodeURIComponent(options.path))
    // #endif

    // #ifdef APP-PLUS
    console.log('1111', decodeURIComponent(decodeURIComponent(options.path)) );
    webviewSrc.value = decodeURIComponent(decodeURIComponent(options.path))
    submitWebviewSignPath(webviewSrc.value)
    // #endif

    // #ifdef MP-ALIPAY || MP-WEIXIN
    webviewSrc.value = decodeURIComponent(options.path)
    submitWebviewSignPath(webviewSrc.value)
    webViewContext = my.createWebViewContext('webview_con');
    // #endif
})


const submitWebviewSignPath = (path: string) => {
    console.log(Store.state.user);
    if(path.indexOf('fadada') > 0) {
        const sysConfig = uni.getSystemInfoSync()
        let phoneInfo = {
            brand: sysConfig.brand,
            model: sysConfig.model,
            platform: sysConfig.platform,
            system: sysConfig.system,
            version: sysConfig.version,
            SDKVersion: uni.SDKVersion,
        }
        addFrontErr({
            errMsg: JSON.stringify({
                msg: '跳转签约',
                UUID: uni.getStorageSync('__DC_STAT_UUID'),
                phone: Store.state.user.info.info.FPhone,
                userId: Store.state.user.account.userId,
                path: path,
                time: new Date().format('yyyy-MM-dd hh:mm:ss'),
                storeNo: Store.state.shop.tempStoreNo,
                ...phoneInfo
            }),
            userId: Store.state.user.account.userId,
            platform: 'zulin_alipay',
            remark: 'zulin alipay webview sign path',
        })
    }
}


const onMessage = (e: any) => {
    let msg = e.detail
    if(msg.api == 'openDocument') {
        uni.showLoading({
            mask: true,
        })
        uni.downloadFile({
            url: msg.data.docUrl,
            fileType: msg.data.type,
            success: (res) => {
                var filePath = res.tempFilePath;
                uni.hideLoading()
                uni.openDocument({
                    filePath: filePath,
                    showMenu: true,
                    success: function (res) {
                        console.log('打开文档成功');
                    }
                });
            },
            complete: () => {
                uni.hideLoading()
            }
        })
    }
    if(msg.api == 'openChooseLocation') {
        uni.chooseLocation({
            success: async (res) => {
                // await interact.modal(JSON.stringify(res))
                webViewContext.postMessage({
                    api: 'chooseLocation',
                    data: res
                })
            }
        })
    }

    console.log(e);

}

</script>

<style lang="scss" scoped>

</style>
